package com.blank.controller;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.blank.model.Express;
import com.blank.model.ExpressStatus;
import com.blank.service.ExpressService;
import com.blank.service.ExpressStatusService;
import com.blank.util.ImportExcel;

@Controller
@RequestMapping("/Express")
public class ExpressController {

	@Resource
	private ExpressService expressService;
	@Resource
	private ExpressStatusService expressStatusService;
	@Resource
	private ImportExcel importExcel;

	@RequestMapping(value = "index", method = { RequestMethod.GET })
	public ModelAndView index(ModelAndView modelAndView) {

		return modelAndView;

	}

	@RequestMapping(value = "search", method = { RequestMethod.POST })
	public ModelAndView search(String nu, ModelAndView modelAndView) {
		modelAndView.setViewName("Express/index");

		if (nu == null || nu == "") {
			return modelAndView;
		} else if (expressService.findByNu(nu) == null) {
			return modelAndView;
		} else {
			ArrayList<ExpressStatus> ess = expressStatusService.getByNu(nu);
			modelAndView.addObject("express", expressService.findByNu(nu));
			modelAndView.addObject("ess", ess);
		}

		return modelAndView;

	}

	@RequestMapping(value = "addRecord", method = { RequestMethod.GET })
	public ModelAndView addRecord(ModelAndView modelAndView, HttpSession session) {
		if (session.getAttribute("userToken") == null)
			return null;
		return modelAndView;
	}

	@RequestMapping(value = "doAddRecord", method = { RequestMethod.POST })
	public ModelAndView doAddRecord(String[] nus, String content, ModelAndView modelAndView, HttpSession session) {
		if (session.getAttribute("userToken") == null)
			return null;
		Express exp;
		try {
			for (String nu : nus) {

				exp = expressService.findByNu(nu);

				if (exp != null) {
					exp.setContent(content);
					expressService.update(exp);
				} else {
					exp = new Express();
					exp.setNu(nu);
					exp.setContent(content);
					expressService.add(exp);
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		modelAndView.setViewName("redirect:/Express/addRecord");
		return modelAndView;
	}

	@RequestMapping(value = "importExcel", method = { RequestMethod.POST })
	@ResponseBody
	public String[][] importExcel(MultipartFile file, ModelAndView modelAndView, HttpServletRequest request) {

		String path = request.getSession().getServletContext().getRealPath("/resources/Upload/");
		if (file == null)
			return null;
		String type = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."),
				file.getOriginalFilename().length());

		if (!(type.equals(".xls") || type.equals(".xlsx")))
			return null;

		String fileName = new Date().getTime() + type;
		File targetFile = new File(path, fileName);

		if (!targetFile.exists()) {
			targetFile.mkdirs();
		}

		// 保存
		try {
			file.transferTo(targetFile);

		} catch (Exception e) {
			return null;
		}

		String[][] result = null;

		try {
			result = importExcel.getData(targetFile, 0);
		} catch (IOException e) {
		}
		return result;

	}

}
